package networking.answerers;

import java.io.IOException;
import java.util.Iterator;
import java.util.Map;

import perceptron.Classifier;
import perceptron.Results;


/**
 * Gère les demandes d'analyse de page.
 * @author simon
 *
 */
public class ClassifyAnswerer {

	public static String generate(Classifier cl, String adress) throws IOException {
		
		/* Effectuer la classification demandée. */
		Results r = cl.classifyPage(adress) ;
		
		/* Génération du XML*/
		String s = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?><response type=\"analyse\">" ;
		s = s + "<class id='"+r.getClass_()+"'/>" ;
		s = s + "<score v='"+r.getScore()+"'/>" ;
		
		/* Ajout des informations relatives aux scores des mots */
		s = s+"<colors>" ;
		Map<String,Integer> word2colorABS = r.getWord2colorABS() ;
		Map<String,Integer> word2colorREL= r.getWord2colorREL() ;
		System.out.println(word2colorABS);System.out.println(word2colorREL);
		System.out.println(word2colorABS.size());System.out.println(word2colorREL.size());
		Iterator<String> it = word2colorREL.keySet().iterator() ;
		while(it.hasNext()) {
			String currentWord = it.next() ;
			int currentAbsColor = word2colorABS.get(currentWord) ;
			int currentRelColor = word2colorREL.get(currentWord) ;
			s=s+"<color word=\""+currentWord+"\"><abs>"+currentAbsColor+"</abs><rel>"+currentRelColor+"</rel></color>" ;
		}
		
		/* Fermeture du xml */
		s=s+"</colors></response>" ;
		
		return s ;
	}
	
}
